package com.aloda.service.impl;

import com.aloda.dto.CategoryPageDTO;
import com.aloda.entity.Category;
import com.aloda.mapper.CategoryMapper;
import com.aloda.result.PageResult;
import com.aloda.service.CategoryService;
import com.aloda.vo.CategoryQueryVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 商品分类分页查询
     *
     * @param categoryPageDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageDTO categoryPageDTO) {
        //转换为北京时区
        if(categoryPageDTO.getBegin()!=null&&categoryPageDTO.getEnd()!=null){
            categoryPageDTO.setBegin(categoryPageDTO.getBegin().plusHours(8));
            categoryPageDTO.setEnd(categoryPageDTO.getEnd().plusHours(8));
        }
        //开始分页查询
        PageHelper.startPage(categoryPageDTO.getPage(),categoryPageDTO.getPageSize());
        Page<Category> page = categoryMapper.pageQuery(categoryPageDTO);
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());
        return pageResult;
    }

    /**
     * 商品页分类选择框查询
     *
     * @return
     */
    @Override
    public List<CategoryQueryVO> query() {
        List<Category> categoryList = categoryMapper.query();
        List<CategoryQueryVO> categoryQueryVOList = new ArrayList<>();
        for (Category category : categoryList) {
            CategoryQueryVO categoryQueryVO = CategoryQueryVO.builder()
                    .id(category.getId())
                    .name(category.getName())
                    .build();
            categoryQueryVOList.add(categoryQueryVO);
        }
        return categoryQueryVOList;
    }
}
